package com.feicheng.iou.controller.login;

import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.entity.Role;
import com.feicheng.iou.entity.User;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.pojo.login.LoginParamPOJO;
import com.feicheng.iou.pojo.login.LoginReturnPOJO;
import com.feicheng.iou.pojo.login.PermissionLevelTwoPOJO;
import com.feicheng.iou.pojo.login.PermissionListPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Console;
import java.util.List;

@RestController
@RequestMapping("/iou/system")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;  //用户业务实现类

    @Autowired
    private RoleService roleService;  //角色业务实现类

    @Autowired
    private PermissionService permissionService; //权限业务实现类

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private LoginReturnPOJO loginReturnPOJO;

    /**
     * @Author wuweinan
     * @Description 登录验证与权限接口
     * @Date 2021-7-5 14:19
     * @param loginParamPOJO
     * @return  ResultInfo
     */
    @PutMapping("/login")
    public ResultInfo queryUserAndPermisson(LoginParamPOJO loginParamPOJO, HttpServletRequest request){
        log.info("LoginController的queryUserAndPermisson方法进入,参数为"+loginParamPOJO);
        //校验用户名和密码
        if(loginParamPOJO.getAccount() == null || loginParamPOJO.getPassword() == null ){
            throw new SystemException(ReturnCode.ACCOUNT_PASSWORD_NOT_NULL);
        }

        //根据账户名密码获取用户信息
        User user = userService.selectUserInfoOfLogin(loginParamPOJO); //封装

        //根据userId获取roleIds中间表
        List<Integer> roleIds = userRoleService.selectRoleIds(user.getUserId()); //中间变量

        //根据RoleIds获取RoleList
        List<Role> roles = roleService.selectRoles(roleIds);  //封装

        //根据roleIds 获取 permissionIds
        List<Integer> permissionIds = rolePermissionService.selectPermissionIdsByRoleIds(roleIds);//中间变量

        //根据permissionIds 获取 permission自关联对象
        List<PermissionListPOJO> permissionListPOJOS = permissionService.selectPermissionsList(permissionIds,false);

        //封装信息
        loginReturnPOJO.setUser(user);
        loginReturnPOJO.setRoles(roles);
        loginReturnPOJO.setPermissions(permissionListPOJOS);
        request.getSession().setAttribute(SessionConstant.CURRENT,loginReturnPOJO);

        return ResultInfo.success(loginReturnPOJO);
    }

    /**
     * @Author wuweinan
     * @Description 登出
     * @Date 2021-7-5 14:19
     * @param request
     * @return  ResultInfo
     */
    @PutMapping("/logout")
    public ResultInfo logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute(SessionConstant.CURRENT);
        return ResultInfo.success("您已成功登出");
    }
}
